Template system reference
When you want to create plugins for mdCMS or if you want to use the mdCMS template system for your own projects you should have a look at this document. It describes the php scripting side of the mdCMS template system. Before you start reading this you should already know this file. (You should know the basics about TPL files. This file is about PHP programming) If you want to use the template system for your own projects please note this: *The mdCMS template system is released under the GNU GPL Link etc *The part about {tpl_plugin:: } will be useless for you (It is mdCMS only!) General usage Of course you will have to include the template system file into your PHP file. This can be done with include('mdcms_template.php'); or require('mdcms_template.php');. You should know the difference between these functions, but it makes no difference for the template system. To get a template parsed you will have to call the template function. To do this you have to use a code like this: mdcms_template($tpl_file, $var_array, $loop_array); $tpl_file has to be the path to the template file. $var_array has to be an array for the Variables. $loop_array has to be an array for the Loops. Note: If you dont want to output a loop you can also use this code for example: mdcms_template($tpl_file, $var_array, " "); (Same if you dont want to use variables) The mdcms_template function will return the result. This means that it does not do any output itself. For the output you will have to echo the returned value. Examples for output 1. To output only one single template you may use this code for example: echo mdcms_template($tpl_file, $var_array, $loop_array); This would directly echo out the parsed template. 2.It is also possible to do it like this: $parsed_tpl_output = mdcms_template($tpl_file, $var_array, $loop_array); echo $parsed_tpl_output; This will give the same result, only that the output was saved in an variable before echoing it out. There is no difference in method 1 or 2. But if you want to use multiple template files (For example a header, body and a footer) you should use this code: $parsed_tpl_output = mdcms_template("header.tpl", $var_array_header, $loop_array_header); //First the header $parsed_tpl_output.= mdcms_template("body.tpl", $var_array_body, $loop_array_body); //Body $parsed_tpl_output.= mdcms_template("footer.tpl", $var_array_footer, $loop_array_footer); //and the footer echo $parsed_tpl_output; //Output them all together In this example all templates are added to the variable $parsed_tpl_output (by using .= ) and the output is done after the php is completed. They are all echoed out together. Variables To set variables you have to set an array ($var_array). Variables example This is the code reference for variables: $var_array'eat' = "apples"; 'eat' can be anything you want. The matching template code looks like this: I like to eat {tpl_var::eat}! The output will be: I like to eat apples! Loops For loops you will need a multi-dimensional array. The easiest example looks like this: $loop_array'loopname'$loopid'variablename' = "Value for the variable"; As you can see you have to set the loop name too. This makes it possible for the template system to seperate the loops (If there are multiple loops in the template). You can also see in the example that $loopid was set. $loopid has to be a number. It seperates the variables for each run. It is important that $loopid has to start with 0 and that no number can be left out! Make sure that the script only counts up $loopid if there was really a variable set for that id! This means that for a loop you will have to set multiple variables with the same name (Else it would not be a loop...). Of course the name for the variable itself has to be set, too. To understand the loops (and $loopid) you should take a look at the following examples! Loops example - one variable Easy loop with 1 variable The PHP code: $loop_array'eating'0'eat' = "Apples"; $loop_array'eating'1'eat' = "Bananas"; For the first run of the loop ($loopid is 0) we give the variable 'eat' the value "Apples". For the second run ($loopid is 1) we set 'eat' to "Bananas". Now follows the template code: What i like to eat: {tpl_loop::eating} -{tpl_var::eat} {tpl_loop_end::eating} As you can see the variable inside the loop was set exactly in the same way as outside a loop. To make it easier to understand here is the final result ouput: What i like to eat: -Apples -Bananas As you can see the code of the loop (-{tpl_var::eat} ) was run 2 times with the 2 different values for the variable 'eat'. Loops example - two (or more) variables If you want to set two (or more) variables you will have to do it like this: $loop_array'eating'0'state' = "Cold"; $loop_array'eating'0'eat' = "Apples"; $loop_array'eating'1'state' = "Fresh"; $loop_array'eating'1'eat' = "Bananas"; For the first run of the loop ($loopid is 0) we give the variable 'eat' the value "Apples" and 'state' becomes "cold". For the second run ($loopid is 1) we set 'eat' to "Bananas" and 'state' becomes "fresh". Now follows the template code: What i like to eat: {tpl_loop::eating} *{tpl_var::state} {tpl_var::eat} {tpl_loop_end::eating} final result ouput: What i like to eat: *Cold Apples *Fresh Bananas This means that different variables inside a loop need to get the same $loopid to be in the same run. Loops example - dynamic content out of mySQL If you want to output something directly from a mySQL table or smililar, you could use this code: We go away from the apples and bananas and get to a usertable now. $loopid = 0; //Set the $loopid to 0 $ab = "SELECT * FROM user_table"; $er = mysql_query($ab); while($row = mysql_fetch_object($er)) { //Assign the values to the loop array $loop_array'users'$loopid'username' = $row->username; $loop_array'users'$loopid'userlevel' = $row->userlevel; $loopid++; //Add 1 to the $loopid! } It is important to set the $loopid to 0 before the mySQL output loop. Then we only have to add 1 to $loopid for every output of mySQl. The template: Username: userlevel: {tpl_loop::users} {tpl_var::username} {tpl_var::userlevel} {tpl_loop_end::users} This will give you an output like this: Username: userlevel: Foo Admin Bar Moderator